local skel = fk.CreateSkill {
  name = "emo__fumeng",
  tags = {Skill.Limited},
  max_game_use_time = 1,
}

Fk:loadTranslationTable{
  ["emo__fumeng"] = "浮梦",
  [":emo__fumeng"] = "限定技，当你进入濒死状态时，你可以将武将牌替换为随机一名不在场上的雀势力武将，将体力上限变更为该武将的体力上限，回复体力至3点，若其有“游戏开始时”的效果，执行之。",

  ["$emo__fumeng_log"] = "浮梦：未找到可用的“雀”势力武将",

  ["$emo__fumeng1"] = "",
  ["$emo__fumeng2"] = "",
}

skel:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return player:usedSkillTimes(skel.name, Player.HistoryGame) < 1
    end
  end,
  on_use = function(self, _, target, player, data)
    local room = player.room
    -- 无视禁表查找
    local generals = {}
    local banned = {"emo__huayubaihuayuqing"}
    for name, general in pairs(Fk.generals) do
      if (general.kingdom == "que" or general.subkingdom == "que") and not table.contains(banned, name) then
        table.insert(generals, name)
      end
    end
    local exists = {}
    for _, p in ipairs(room.players) do
      table.insert(exists, p.general)
      if p.deputyGeneral ~= "" then
        table.insert(exists, p.deputyGeneral)
      end
    end
    generals = table.filter(generals, function(name) return not table.contains(exists, name) end)
    if #generals == 0 then
      room:sendLog{ type = "$emo__fumeng_log", toast = true }
      return
    end
    local name = table.random(generals)
    local general = Fk.generals[name]
    room:setPlayerProperty(player, "maxHp", general.maxHp)
    room:changeKingdom(player, "que", false)
    room:changeHero(player, name, false, false, true, false, false)
    room:recover { num = 3 - player.hp, skillName = skel.name, who = player, recoverBy = player }
    if player.dead then return end
    local skills = table.filter(general:getSkillNameList(true), function (s)
      return player:hasSkill(s, true)
    end)
    local startSkills = {}---@type TriggerSkill[]
    local start_events = {fk.GamePrepared, fk.GameStart}
    for _, sname in ipairs(skills) do
      local s = Fk.skills[sname]
      local slist = {s}
      table.insertTable(slist, s.related_skills)
      for _, skill in ipairs(slist) do
        if skill:isInstanceOf(TriggerSkill) and skill.event and table.contains(start_events, skill.event) then
          table.insertIfNeed(startSkills, skill)
        end
      end
    end
    if #startSkills == 0 then return end
    for _, event in ipairs(start_events) do
      local event_data = {}
      local event_obj = event:new(room, target, event_data)
      for _, skill in ipairs(startSkills) do
        if skill.event == event then
          if not skill.late_refresh and skill:canRefresh(event_obj, target, target, event_data) then
            skill:refresh(event_obj, target, target, event_data)
          end
          if skill:triggerable(event_obj, target, target, event_data) then
            skill:trigger(event_obj, target, target, event_data)
          end
          if skill.late_refresh and skill:canRefresh(event_obj, target, target, event_data) then
            skill:refresh(event_obj, target, target, event_data)
          end
        end
      end
    end
  end,
})



return skel
